/**
 * Description:
 * Class Name:CrazyItObjectFactory2
 * Date:2022/5/1 13:44
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第18章_类加载机制与反射._18_6_反射和泛型._18_6_1_泛型和Class类;

import javax.swing.*;
import java.util.Date;

public class CrazyItObjectFactory2 {
    public static <T> T getInstance(Class<T> cls){
        try {
            return cls.getConstructor().newInstance();
        }
        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        // 获取实例后无须类型转换
        Date d = CrazyItObjectFactory2.getInstance(Date.class);
        JFrame f = CrazyItObjectFactory2.getInstance(JFrame.class);
    }
}
